package com.lantosec.hmrp.modules.morality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.morality.entity.*;
import com.lantosec.hmrp.modules.morality.mapper.MoralityPlanMapper;
import com.lantosec.hmrp.modules.morality.service.IMoralityNoteSelfService;
import com.lantosec.hmrp.modules.morality.service.IMoralityNoteService;
import com.lantosec.hmrp.modules.morality.service.IMoralityPlanLevelService;
import com.lantosec.hmrp.modules.morality.service.IMoralityPlanService;
import com.lantosec.hmrp.modules.morality.vo.MoralityPlanVO;
import com.lantosec.hmrp.modules.system.entity.SysDept;
import com.lantosec.hmrp.modules.system.entity.SysDeptUser;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 考评方案 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-16
 */
@Service
@Primary
@Log4j2
public class MoralityPlanServiceImpl extends ServiceImpl<MoralityPlanMapper, MoralityPlan> implements IMoralityPlanService {
    @Autowired
    private IMoralityNoteService iMoralityNoteService;

    @Autowired
    private IMoralityNoteSelfService iMoralityNoteSelfService;

    @Autowired
    private IMoralityPlanLevelService iMoralityPlanLevelService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveMoralityPlan(MoralityPlanList moralityPlanList) {
        //获得考评方案基本信息
        MoralityPlan moralityPlan = moralityPlanList.getMoralityPlan();
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String userName = TokenUtil.getName();
        moralityPlan.setCreateUserId(userId);
        moralityPlan.setCreateUser(userName);
        moralityPlan.setCreateTime(DateUtils.nowDate());
        this.save(moralityPlan);

        //获得考评方案选择的基本项目
        List<MoralityPlanItem> moralityPlanItemList = moralityPlanList.getMoralityPlanItemList();
        moralityPlanItemList.forEach(parm -> {
            //获得考评方案id
            parm.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            parm.setMoralityItemType(1);
            baseMapper.saveMoralityPlanItem(parm);
        });

        //获得考评方案选择的加减分项目
        List<MoralityPlanItem> moralityPlanItemList1 = moralityPlanList.getMoralityPlanItemList1();
        moralityPlanItemList1.forEach(parm -> {
            //获得考评方案id
            parm.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            parm.setMoralityItemType(3);
            baseMapper.saveMoralityPlanItem(parm);
        });

        //获得考评方案等级
        List<MoralityPlanLevel> moralityPlanLevels = moralityPlanList.getMoralityPlanLevelList();
        moralityPlanLevels.forEach(param->{
            param.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            iMoralityPlanLevelService.save(param);
        });

        //获得考评方案的被考评人
        List<MoralityPlanTarget> moralityPlanTargetList = moralityPlanList.getMoralityPlanTargetList();
        //初始化记录表 和 初始化记录明细表
        saveMoralityInfo(moralityPlan, moralityPlanItemList, moralityPlanItemList1, moralityPlanTargetList);

        return moralityPlan.getMoralityPlanId();
    }

    private void saveMoralityInfo(MoralityPlan moralityPlan, List<MoralityPlanItem> moralityPlanItemList, List<MoralityPlanItem> moralityPlanItemList1, List<MoralityPlanTarget> moralityPlanTargetList) {
        moralityPlanTargetList.forEach(parm -> {
            parm.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            String[] targetIds = parm.getTargetId().split("-");
            parm.setTargetId(targetIds[1]);
            parm.setTargetDeptId(targetIds[0]);
            baseMapper.saveMoralityPlanTarget(parm);

            //初始化记录表（morality_note）
            MoralityNote moralityNote = new MoralityNote();
            moralityNote.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            moralityNote.setMoralityTargetId(targetIds[1]);
            moralityNote.setCreateTime(DateUtils.nowDate());
            moralityNote.setTargetDeptId(targetIds[0]);
            moralityNote.setCreateTime(DateUtils.nowDate());
            // 查询被考评人所属党支部
            List<MoralityBranchDept> moralityBranchDepts = baseMapper.getBranchDept(null,null,targetIds[0]);
            if(null != moralityBranchDepts && moralityBranchDepts.size() > 0){
                String branchId = moralityBranchDepts.get(0).getBranchId();
                moralityNote.setTargetBranchId(branchId);
            }
            iMoralityNoteService.save(moralityNote);
            //初始化记录明细表morality_note_self
            //初始化基本项目
            moralityPlanItemList.forEach(parm1 -> {
                MoralityNoteSelf moralityNoteSelf = new MoralityNoteSelf();
                moralityNoteSelf.setMoralityPlanId(moralityPlan.getMoralityPlanId());
                moralityNoteSelf.setMoralityNoteId(moralityNote.getMoralityNoteId());
                moralityNoteSelf.setMoralityTargetId(targetIds[1]);
                moralityNoteSelf.setMoralityItemId(parm1.getMoralityItemId());
                iMoralityNoteSelfService.save(moralityNoteSelf);
            });
            //初始化一票认定差项目
            moralityPlanItemList1.forEach(parm2 -> {
                MoralityNoteSelf moralityNoteSelf = new MoralityNoteSelf();
                moralityNoteSelf.setMoralityPlanId(moralityPlan.getMoralityPlanId());
                moralityNoteSelf.setMoralityNoteId(moralityNote.getMoralityNoteId());
                moralityNoteSelf.setMoralityTargetId(targetIds[1]);
                moralityNoteSelf.setMoralityItemId(parm2.getEvalItemId());
                iMoralityNoteSelfService.save(moralityNoteSelf);
            });
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMoralityPlan(MoralityPlanList moralityPlanList) {
        //获得考评方案基本信息
        MoralityPlan moralityPlan = moralityPlanList.getMoralityPlan();
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String userName = TokenUtil.getName();
        moralityPlan.setEditorUserId(userId);
        moralityPlan.setEditorUser(userName);
        moralityPlan.setEditorTime(DateUtils.nowDate());
        this.updateById(moralityPlan);

        //修改考评方案项目，先删后增
        baseMapper.deleteMoralityPlanItem(moralityPlan.getMoralityPlanId());
        //获得考评方案选择的基本项目
        List<MoralityPlanItem> moralityPlanItemList = moralityPlanList.getMoralityPlanItemList();
        moralityPlanItemList.forEach(parm -> {
            //获得考评方案id
            parm.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            parm.setMoralityItemType(1);
            baseMapper.saveMoralityPlanItem(parm);
        });
        //获得考评方案选择的加减分项目
        List<MoralityPlanItem> moralityPlanItemList1 = moralityPlanList.getMoralityPlanItemList1();
        moralityPlanItemList1.forEach(parm -> {
            //获得考评方案id
            parm.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            parm.setMoralityItemType(3);
            baseMapper.saveMoralityPlanItem(parm);
        });
        //获得考评方案等级
        List<MoralityPlanLevel> moralityPlanLevels = moralityPlanList.getMoralityPlanLevelList();
        baseMapper.deleteMoralityPlanLevel(moralityPlan.getMoralityPlanId());
        moralityPlanLevels.forEach(param->{
            param.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            iMoralityPlanLevelService.save(param);
        });

        //获得考评方案的被考评人
        List<MoralityPlanTarget> moralityPlanTargetList = moralityPlanList.getMoralityPlanTargetList();
        //修改考评方案考评对象，先删后增
        baseMapper.deleteMoralityPlanTarget(moralityPlan.getMoralityPlanId());
        moralityPlanTargetList.forEach(parm -> {
            parm.setMoralityPlanId(moralityPlan.getMoralityPlanId());
            String[] targetIds = parm.getTargetId().split(",");
            parm.setTargetId(targetIds[1]);
            parm.setTargetDeptId(targetIds[0]);
            baseMapper.saveMoralityPlanTarget(parm);
        });
    }

    @Override
    public List<MoralityPlanVO> getAll(PageVO<MoralityPlanVO> page, String moralityPlanName, Integer moralityCycleId) {
        //查询考评方案基本信息
        List<MoralityPlanVO> moralityPlanVOS = baseMapper.getMoralityPlanList(page, moralityPlanName, moralityCycleId);
        moralityPlanVOS.forEach(parm -> {
            //查询考评方案下的考评基本项目
            Integer moralityPlanId = parm.getMoralityPlanId();
            List<MoralityPlanItem> moralityPlanItemList = baseMapper.getMoralityPlanItem(moralityPlanId);
            //查询考评方案下的考评加减分项目
            List<MoralityPlanItem> moralityPlanItemList1 = baseMapper.getMoralityPlanItem1(moralityPlanId);
            //查询考评方案下的等级
            List<MoralityPlanLevel> moralityPlanLevels =  iMoralityPlanLevelService.list(new QueryWrapper<MoralityPlanLevel>().eq(MoralityPlanLevel.ColName.morality_plan_id.toString(),moralityPlanId));
            //查询考评方案下的被考评人
            List<MoralityPlanTarget> moralityPlanTargetList = baseMapper.getMoralityPlanTarget(moralityPlanId);
            moralityPlanTargetList.forEach(param->{
                //返回人员的部门
                String targetId = param.getTargetId();
                String deptId = param.getTargetDeptId();
                param.setTargetId(param.getTargetDeptId()+"-"+targetId);
                if(StringUtils.isNotEmpty(deptId)){
                    SysDept sysDept = iSysDeptService.getById(deptId);
                    String deptName = sysDept.getDeptName();
                    if(StringUtils.isNotEmpty(deptName)){
                        param.setName(deptName+"-"+param.getName());
                    }
                }
            });
            parm.setMoralityPlanItemList(moralityPlanItemList);
            parm.setMoralityPlanItemList1(moralityPlanItemList1);
            parm.setMoralityPlanTargetList(moralityPlanTargetList);
            parm.setMoralityPlanLevels(moralityPlanLevels);
        });
        page.setRecords(moralityPlanVOS);
        return moralityPlanVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMoralityPlan(Integer moralityPlanId) {
        //删除考评方案基本信息
        this.removeById(moralityPlanId);
        //删除考评方案下的考评基本项目
        baseMapper.deleteMoralityPlanItem(moralityPlanId);
        //删除考评方案下的被考评人
        baseMapper.deleteMoralityPlanTarget(moralityPlanId);
        baseMapper.deleteMoralityPlanLevel(moralityPlanId);
    }

    @Override
    public void saveBranchDept(List<MoralityBranchDept> moralityBranchDept) {
        moralityBranchDept.forEach(param->{
            baseMapper.saveBranchDept(param);
        });
    }

    @Override
    public List<MoralityBranchDept> getBranchDept(PageVO<MoralityBranchDept> page, String branchId, String deptId) {
        List<MoralityBranchDept> moralityBranchDepts = baseMapper.getBranchDept(page,branchId,deptId);
        moralityBranchDepts.forEach(param->{
            String branchIds = param.getBranchId();
            List<MoralityBranchDept> deptName = baseMapper.getBranchDeptName(branchIds);
            param.setDeptNameList(deptName);
        });
        page.setRecords(moralityBranchDepts);
        return moralityBranchDepts;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBranchDept(MoralityBranchDept moralityBranchDept) {
        String branchId = moralityBranchDept.getBranchId();
        baseMapper.deleteBranchDept(branchId);
        List<MoralityBranchDept> deptNameList = moralityBranchDept.getDeptNameList();
        deptNameList.forEach(param->{
            baseMapper.saveBranchDept(param);
        });
    }

    @Override
    public void deleteBranchDept(String branchId) {
        baseMapper.deleteBranchDept(branchId);
    }

    @Override
    public List<String> getBranchOfDeptId() {
        List<String> moralityBranchDepts = baseMapper.getBranchOfDeptId();
        return moralityBranchDepts;
    }
}
